package com.ezops.poc.dao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.ezops.poc.bo.ClientBondFileModel;
import com.ezops.poc.bo.ClientEquityFileModel;
import com.ezops.poc.bo.ot.ClientOTEquityModel;

/**
 * data access layer for accesssing the client data. (Can be trating as staging
 * layer.)
 * 
 * @author vgaurav
 * 
 */
@Repository(value = "clientDAO")
public class ClientModelDAO {

	private static final String COMMA_SEPARATED_COLUMNS = "TRADEID,FILENAME,CLIENTID,STATUS,ACTION,EXTERNALREFID,INTERNALREFID,COUNTERPARTY,COUNTERPARTYROLE,BOOK,BUNDLETYPE,BUNDLENAME,"
			+ "HOLIDAYCODE,CURRENCY,TRADESETTLEMENTCURRENCY,PRODUCTCODETYPE,PRODUCTCODEVALUE,NOMINAL,BUYSELL,TRADERNAME,SALESPERSON,TRADEDATE,SETTLEMENTDATE,NEGOTIATEDPRICETYPE,NEGOTIATEDPRICE,ACCRUAL,FXRATE,"
			+ "TEMPLATENAME,MIRRORBOOK,MARKETTYPE,FEETYPE,FEEDIRECTION,FEEAMOUNT,FEECCY,FEELEGALENTITY,FEESTARTDATE,"
			+ "FEEENDDATE,FEEKNOWNDATE,FEEDATE,KEYWORDLONGSHORT,KEYWORDTRADESOURCE";

	private static final String INSERT_CLIENT_MODEL_QUERY = "INSERT INTO EZOPSDB.CLIENT_MODEL( "
			+ COMMA_SEPARATED_COLUMNS
			+ ")"
			+ " VALUES "
			+ "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

	private static final Logger log = LoggerFactory
			.getLogger(ClientModelDAO.class);

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public ClientModelDAO() {
	}

	public void persist(ClientBondFileModel transientModel) {
		log.info("Adding trade record to database. trade id: "
				+ transientModel.getTradeId() + " action: "
				+ transientModel.getAction());
		jdbcTemplate.update(INSERT_CLIENT_MODEL_QUERY,
				getColumnValues(transientModel));
	}

	public void persist(ClientEquityFileModel transientModel) {
		log.info("Adding trade record to database. trade id: "
				+ transientModel.getTradeId() + " action: "
				+ transientModel.getAction());
		jdbcTemplate.update(INSERT_CLIENT_MODEL_QUERY,
				getColumnValues(transientModel));
	}

	public void persist(ClientOTEquityModel transientModel) {
		log.info("Adding trade record to database. action:+ "
				+ transientModel.getAction());
	//	jdbcTemplate.update(INSERT_CLIENT_MODEL_QUERY,
	//			getColumnValues(transientModel));
	}

	/**
	 * 
	 * @param model
	 * @return
	 */
	private Object[] getColumnValues(ClientBondFileModel model) {
		return new Object[] { model.getTradeId(), model.getFileName(),
				model.getClientId(), model.getStatus(), model.getAction(),
				model.getExternalRefId(), model.getInternalRefId(),
				model.getCounterparty(), model.getBook(),
				model.getBundleType(), model.getBundleName(),
				model.getHolidayCode(), model.getCurrency(),
				model.getTradeSettlementCurrency(), model.getProductCodeType(),
				model.getProductCodeValue(), model.getNominal(),
				model.getBuySell(), model.getTraderName(),
				model.getSalesPerson(), model.getTradeDate(),
				model.getSettlementDate(), model.getNegotiatedPriceType(),
				model.getNegotiatedPrice(), model.getAccrual(),
				model.getAccrual(), model.getFxRate(), model.getTemplateName(),
				model.getMirrorBook(), model.getMarketType(),
				model.getFeeType(), model.getFeeDirection(),
				model.getFeeAmount(), model.getFeeCcy(),
				model.getFeeLegalEntity(), model.getFeeStartDate(),
				model.getFeeEndDate(), model.getFeeKnownDate(),
				model.getFeeDate(), model.getKeywordLongShort(),
				model.getKeywordTradeSource() };

	}

	/**
	 * @TODO - Should go in separate table
	 * @param model
	 * @return
	 */
	private Object[] getColumnValues(ClientEquityFileModel model) {
		return new Object[] { model.getTradeId(), model.getFileName(),
				model.getClientId(), model.getStatus(), model.getAction(),
				model.getExternalRefId(), model.getInternalRefId(),
				model.getCounterparty(), model.getBook(),
				model.getBundleType(), model.getBundleName(),
				model.getHolidayCode(), model.getCurrency(),
				model.getTradeSettlementCurrency(), model.getProductCodeType(),
				model.getProductCodeValue(), null, model.getBuySell(), null,
				null, model.getTradeDate(), model.getSettlementDate(), null,
				null, null, null, null, null, null, null, model.getFeeType(),
				model.getFeeDirection(), model.getFeeAmount(),
				model.getFeeCcy(), model.getFeeLegalEntity(),
				model.getFeeStartDate(), model.getFeeEndDate(),
				model.getFeeKnownDate(), model.getFeeDate(), null,
				model.getKeywordTradeSource() };

	}

	private Object[] getColumnValues(ClientOTEquityModel model) {
		return new Object[] { 0, model.getFileName(), model.getClientId(),
				model.getStatus(), model.getAction(), 0, 0, model.getPartyId(),
				model.getBookId(), null, null, null, null, null, null, null,
				null, model.getBuySell(), null, null,
				model.getSettlementDate(), model.getSettlementDate(), null,
				null, null, null, null, null, null, null, null, null, null,
				null, null, null, null, null, null, null, null };

	}
}
